//
//  NewsCell.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "NewsCell.h"
#import <Masonry.h>
#import "Commont.h"
#import <UIImageView+WebCache.h>


@interface NewsCell()

@property(nonatomic,strong)UIImageView *itemImageView;
@property(nonatomic,strong)UILabel *itemDescLabel;
@property(nonatomic,strong)UILabel *itemDateLabel;



@end

@implementation NewsCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.newsCellVM = [[NewsCellVM alloc]init];
        [self.newsCellVM addObserver:self forKeyPath:@"coverImg" options:NSKeyValueObservingOptionNew context:nil];
        [self.newsCellVM addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
        [self.newsCellVM addObserver:self forKeyPath:@"dateStr" options:NSKeyValueObservingOptionNew context:nil];

        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.itemImageView = [[UIImageView alloc]init];
        self.itemImageView.image = [UIImage imageNamed:@"default_banner"];
        self.itemImageView.layer.cornerRadius = 4.0f;
        self.itemImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.itemImageView];
        [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(20);
            make.right.equalTo(self.contentView).offset(-20);
            make.height.equalTo(self.itemImageView.mas_width).multipliedBy(0.29);
        }];
        
        self.itemDescLabel = [[UILabel alloc]init];
        self.itemDescLabel.text = @"详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情详情";
        self.itemDescLabel.numberOfLines = 0;
        self.itemDescLabel.textColor = RGBHex(0x263238);
        self.itemDescLabel.font = [UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:self.itemDescLabel];
        [self.itemDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.itemImageView).offset(12);
            make.right.equalTo(self.itemImageView).offset(-12);
            make.top.equalTo(self.itemImageView.mas_bottom).offset(12);
        }];
        
        self.itemDateLabel = [[UILabel alloc]init];
        self.itemDateLabel.text = @"2019-01-01";
        self.itemDateLabel.textColor = RGBHex(0x90A4AE);
        self.itemDateLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.itemDateLabel];
        [self.itemDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.itemDescLabel);
            make.top.equalTo(self.itemDescLabel.mas_bottom).offset(12);
            make.bottom.equalTo(self.contentView).offset(-14.5);
        }];
        
        
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"coverImg"]) {
        NSString *coverImg = value;
        [self.itemImageView sd_setImageWithURL:[NSURL URLWithString:coverImg] placeholderImage:[UIImage imageNamed:@"default_banner"]];
        
    }else if ([keyPath isEqualToString:@"title"]){
        NSString *title = value;
        self.itemDescLabel.text = title;
        
    }else if ([keyPath isEqualToString:@"dateStr"]){
        NSString *dateStr = value;
        self.itemDateLabel.text = dateStr;
    }
}

- (void)dealloc{
    [self.newsCellVM removeObserver:self forKeyPath:@"coverImg"];
    [self.newsCellVM removeObserver:self forKeyPath:@"title"];
    [self.newsCellVM removeObserver:self forKeyPath:@"dateStr"];

}


@end
